What is @turf/difference?
@turf/difference is a module in the Turf.js library that allows you to compute the geometric difference between two polygons. This is useful in geospatial analysis for determining the area that is unique to one polygon when compared to another.
What are @turf/difference's main functionalities?
Compute Difference Between Two Polygons
This feature allows you to compute the difference between two polygons. The resulting polygon represents the area that is unique to the first polygon when compared to the second polygon.
const turf = require('@turf/turf');
const polygon1 = turf.polygon([[
[-82.574787, 35.594087],
[-82.574787, 35.615581],
[-82.545261, 35.615581],
[-82.545261, 35.594087],
[-82.574787, 35.594087]
]]);
const polygon2 = turf.polygon([[
[-82.560024, 35.585153],
[-82.560024, 35.602602],
[-82.52964, 35.602602],
[-82.52964, 35.585153],
[-82.560024, 35.585153]
]]);
const difference = turf.difference(polygon1, polygon2);
console.log(difference);
Other packages similar to @turf/difference
jsts
JSTS is a JavaScript library of spatial predicates and functions for processing geometry. It is a port of the Java Topology Suite (JTS). JSTS provides a more comprehensive set of geometric operations compared to @turf/difference, including union, intersection, and buffer operations.
martinez-polygon-clipping
Martinez Polygon Clipping is a library for performing boolean operations on polygons, such as union, intersection, and difference. It is known for its performance and accuracy, making it a good alternative to @turf/difference for complex polygon operations.
@turf/difference
difference
Finds the difference between two polygons by clipping the second
polygon from the first.
Parameters
Examples
var poly1 = {
"type": "Feature",
"properties": {
"fill": "#0f0"
},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-46.738586, -23.596711],
[-46.738586, -23.458207],
[-46.560058, -23.458207],
[-46.560058, -23.596711],
[-46.738586, -23.596711]
]]
}
};
var poly2 = {
"type": "Feature",
"properties": {
"fill": "#00f"
},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-46.650009, -23.631314],
[-46.650009, -23.5237],
[-46.509246, -23.5237],
[-46.509246, -23.631314],
[-46.650009, -23.631314]
]]
}
};
var differenced = turf.difference(poly1, poly2);
differenced.properties.fill = '#f00';
var polygons = {
"type": "FeatureCollection",
"features": [poly1, poly2]
};
Returns Feature<(Polygon | MultiPolygon)> a Polygon or MultiPolygon feature showing the area of p1
excluding the area of p2
This module is part of the Turfjs project, an open source
module collection dedicated to geographic algorithms. It is maintained in the
Turfjs/turf repository, where you can create
PRs and issues.
Installation
Install this module individually:
$ npm install @turf/difference
Or install the Turf module that includes it as a function:
$ npm install @turf/turf